// pages/uploadvideo/uploadvideo.js
var api = require('../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    temImagePath: '',
    apptoken:'',
    priceindex: 0,
    videoProgress:0,
    backdrop:{},
    notice:[],
    filename:'',
    pathname:'',
    average:'',
    priceArr: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    isShow:false,
    temvideoPath:'',
    temImagePath:'',
    bid:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.videoinit();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //选择图片
  ChonicePhoto: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认1
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var temps;
        temps = res.tempFilePaths;
        that.setData({
          temImagePath: temps[0],
          isShow: true
        });
        wx.showLoading({
          title: '上传中...'
        });
        wx.uploadFile({
          url: 'https://cum.api.livestaring.com/v001/About/upload',
          filePath: that.data.temImagePath,
          name: 'file',
          header: {'content-type': 'multipart/form-data'},
          formData: {
            'token': that.data.apptoken,
          },
          success: function (res) {
            var severObj = JSON.parse(res.data)
            if (res.statusCode = 200) {
              if (severObj.code = 200) {
                wx.showToast({
                  title: '图片上传成功',
                  icon: 'none'
                })
                var addBkImg = that.addBackGroundImage(severObj.data.file, severObj.data.path);
              }
            }
          },
          fail: function (res) {
            wx.hideLoading();
            wx.showToast({
              title: res.errMsg,
              icon: 'none'
            })
          },
          complete: function (res) {
            wx.hideLoading();
            wx.showToast({
              title: res.errMsg,
              icon: 'none'
            })
          }
        })
      }
    })
  },
  //添加背景图片
  addBackGroundImage: function (file, path) {
    var that = this;
    api.post({
      url: 'Backdrop/add',
      data: {
        file: file,
        path: path,
        token: that.data.apptoken,
        status: 2
      },
      success: data => {
        if (data.code == 200) {
          wx.showToast({
            title: data.data.message,
            icon: 'none'
          })
          that.setData({
            bid: data.data.bid
          })
        } else if (data.code == 400) {
          wx.showToast({
            title: data.data.message,
            icon: 'none'
          })
        }
      },
      complete: function (res) {
        wx.hideLoading();
      }
    })
  },
  //价格选择
  bindPickerPriceChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      priceindex: e.detail.value
    })
    var that = this;
    that.data.livePrice = that.data.priceArr[that.data.priceindex]
  },
  onFileName(e) {
    this.setData({
      filename:e.detail.value
    })
  },
  onPathName(e){
    this.setData({
      pathname: e.detail.value
    })
  },

  //视频初始
  videoinit:function(e) {
    var that = this;
    try {
      var value = wx.getStorageSync('token')
      if (value) {
        that.setData({
          apptoken: value
        })
      }
    } catch (e) {
    }
    api.post({
      url: 'Video/initial',
      data: {
        'token':that.data.apptoken
      },
      success: data => {
        if (data.code == 200) {
          var show;
          if (data.data.backdrop.bid) {
            show = true;
          } else {
            show = false;
          }
          that.setData({
            average: data.data.average,
            notice:data.data.notice,
            isShow: show,
            temImagePath: data.data.backdrop.image,
            bid: data.data.backdrop.bid
          })
        } else if (data.code == 300) {
          wx.showToast({
            title: data.data.message,
            icon: 'none'
          })
        } else if (data.code == 400) {
          wx.showToast({
            title: data.data.message,
            icon: 'none'
          })
        }
      }
    })
  },
  //选择视频
  choiceVideo: function (e) {
    var that = this;
    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      maxDuration: 60,
      camera: 'back',
      compressed: true,
      success: function (res) {
        that.setData({
          temvideoPath: res.tempFilePath
        })
      }
    })
  },
  //上传视频
  uploadVideo:function () {
    var that = this;
    const uploadTask = wx.uploadFile({
      url: 'https://cum.api.livestaring.com/v001/About/upload',
      filePath: that.data.temvideoPath,
      name: 'file',
      formData: {
        'token': that.data.apptoken,
        'content-type': 'multipart/form-data'
      },
      success: function (res) {
        var severObj = JSON.parse(res.data)
        if (res.statusCode = 200) {
          if (severObj.code = 200) {
            wx.showToast({
              title: '视频上传成功',
              icon: 'none',
            })
            that.setData({
              filename: severObj.data.file,
              pathname: severObj.data.path
            })
          }
        }
      }
    })
    uploadTask.onProgressUpdate((res) => {
      that.setData({
        videoProgress: res.progress
      })
      console.log('上传进度', res.progress)
      console.log('已经上传的数据长度', res.totalBytesSent)
      console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
    })
  },

  //立即发布
  formSubmit(e) {
    var that = this;
    if(!that.data.filename||!that.data.pathname) {
      wx.showToast({
        title: '请先上传视频',
        icon:'none'
      })
      return;
    }
    if(!e.detail.value.name) {
      wx.showToast({
        title: '请先输入标题',
        icon: 'none'
      })
      return;
    }
    e.detail.value.token = that.data.apptoken;
    api.post({
      url: 'Video/add',
      data: e.detail.value,
      success: data => {
        if (data.code == 200) {
          wx.showToast({
            title: data.data.message,
            icon:'none',
            complete: function (res) {
              wx.navigateBack();
            }
          })
        } else if (data.code == 300) {
          wx.showToast({
            title: data.data.message,
            icon: 'none'
          })
        } else if (data.code == 400) {
          wx.showToast({
            title: data.data.message,
            icon: 'none'
          })
        }
      }
    })
  }
})